<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>树链剖分详解 AND 题解 P6098 【[USACO19FEB]Cow Land G】 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="毒瘤题目千千万，DP树剖各一半。">
<meta property="og:type" content="article">
<meta property="og:title" content="树链剖分详解 AND 题解 P6098 【[USACO19FEB]Cow Land G】">
<meta property="og:url" content="http://example.com/2020/08/28/%E6%A0%91%E9%93%BE%E5%89%96%E5%88%86%E8%AF%A6%E8%A7%A3%20AND%20%E9%A2%98%E8%A7%A3%20P6098%20%E3%80%90[USACO19FEB]Cow%20Land%20G%E3%80%91/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="毒瘤题目千千万，DP树剖各一半。">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://cdn.luogu.com.cn/upload/image_hosting/wvcn1s57.png">
<meta property="og:image" content="https://cdn.luogu.com.cn/upload/image_hosting/sa0fpyz9.png">
<meta property="og:image" content="https://cdn.luogu.com.cn/upload/image_hosting/3w1daj6g.png">
<meta property="article:published_time" content="2020-08-28T06:51:56.217Z">
<meta property="article:modified_time" content="2023-07-19T11:50:42.882Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.luogu.com.cn/upload/image_hosting/wvcn1s57.png">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-树链剖分详解 AND 题解 P6098 【[USACO19FEB]Cow Land G】" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2020/08/28/%E6%A0%91%E9%93%BE%E5%89%96%E5%88%86%E8%AF%A6%E8%A7%A3%20AND%20%E9%A2%98%E8%A7%A3%20P6098%20%E3%80%90%5BUSACO19FEB%5DCow%20Land%20G%E3%80%91/" class="article-date">
  <time class="dt-published" datetime="2020-08-28T06:51:56.217Z" itemprop="datePublished">2020-08-28</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      树链剖分详解 AND 题解 P6098 【[USACO19FEB]Cow Land G】
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>毒瘤题目千千万，DP树剖各一半。</p>
<p>看到各位大佬们已经把其他的东西讲的很明白了，我这个 juruo 就讲一讲最基本的树链剖分吧。</p>
<h2 id="0-树剖是什么？能吃吗？"><a href="#0-树剖是什么？能吃吗？" class="headerlink" title="0.树剖是什么？能吃吗？"></a>0.树剖是什么？<del>能吃吗？</del></h2><p><del>不能吃</del></p>
<p>树剖是树链剖分的简称，我们一般说的树剖其实指<strong>重链剖分</strong>。当然，还有一种长链剖分<del>我不会</del>，但是据说不常用。</p>
<p>树链剖分能够把树剖分成许多链，这样就可以用维护区间的方式维护一棵树。</p>
<h2 id="1-怎么剖分"><a href="#1-怎么剖分" class="headerlink" title="1.怎么剖分"></a>1.怎么剖分</h2><p>先引入一些概念：</p>
<ol>
<li><strong>重儿子</strong>：一棵树最大的子树叫重儿子。例如这棵树中3就是1的重儿子：<img src="https://cdn.luogu.com.cn/upload/image_hosting/wvcn1s57.png" alt="">很明显，一棵树的重儿子是唯一的。什么？有多棵子树的大小相同？那就随便选一个呗。</li>
<li><strong>轻儿子</strong>：除了重儿子都是轻儿子。<del>废话</del></li>
<li><strong>重边</strong>：连接父亲和重儿子的边就是重边。</li>
<li><strong>轻边</strong>：除了重边都是轻边。</li>
<li><strong>重链</strong>：许多重边连起来就叫重链。例如：<img src="https://cdn.luogu.com.cn/upload/image_hosting/sa0fpyz9.png" alt=""></li>
</ol>
<p>这棵树里节点 ${1,3,5,6}$ 可以构成一颗重链。<del>很显然</del>  ，<strong>每个重链的起点一定是一个轻儿子。每个节点都属于且仅属于一条重链。</strong>&lt;-很重要，一定要记住！</p>
<p>然后就开始剖分了。</p>
<p>具体的剖分过程，就是维护一些数组：</p>
<ul>
<li>$deep[i]$ 代表节点 $i$ 的深度。</li>
<li>$top[i]$ 代表节点 $i$ 所属重链的链顶。（也就是重链里深度最小的那个节点）</li>
<li>$size[i]$ 代表以 $i$ 为根的子树的大小。</li>
<li>$son[i]$ 代表节点 $i$ 的唯一一个重儿子是谁。</li>
<li>$f[i]$ 代表节点 $i$ 的父亲是谁。</li>
<li>$dfn[i]$ 代表节点 $i$ 的”遍历顺序“。</li>
</ul>
<p>剖分时要跑两个dfs。<del>经典操作</del></p>
<p>第一个dfs要维护 $size$ 、$son$ 、$f$、$deep$ 这几个数组。</p>
<p>提示：树要用无向图存！</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">dfs1</span><span class="params">(<span class="type">int</span> u,<span class="type">int</span> fa<span class="comment">/*记录当前节点父亲是谁*/</span>)</span></span>&#123;</span><br><span class="line">    size[u]=<span class="number">1</span>;<span class="comment">//因为自己也是子树的一部分</span></span><br><span class="line">    f[u]=fa;</span><br><span class="line">    deep[u]=deep[fa]+<span class="number">1</span>;<span class="comment">//很明显，当前深度=父亲深度+1</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;g[u].<span class="built_in">size</span>();i++)&#123;</span><br><span class="line">        <span class="type">int</span> v=g[u][i];<span class="comment">//遍历每个出边</span></span><br><span class="line">        <span class="keyword">if</span>(v!=fa)&#123;<span class="comment">//如果当前出边终点是儿子而不是父亲</span></span><br><span class="line">            <span class="built_in">dfs1</span>(v,u);<span class="comment">//搜</span></span><br><span class="line">            size[u]+=size[v];<span class="comment">//加上儿子大小</span></span><br><span class="line">            <span class="keyword">if</span>(size[v]&gt;size[son[u]])&#123;<span class="comment">//找到最大的儿子作为重儿子</span></span><br><span class="line">                son[u]=v;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后我们已经知道了每个节点的重儿子，现在应该把它们连起来成为一条重链了：<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">dfs2</span><span class="params">(<span class="type">int</span> u,<span class="type">int</span> tp<span class="comment">/*当前链顶*/</span>)</span></span>&#123;</span><br><span class="line">    top[u]=tp;</span><br><span class="line">    dfn[u]=++step;</span><br><span class="line">    <span class="keyword">if</span>(son[u])&#123;<span class="comment">//如果没有重儿子，那么一个儿子也没有</span></span><br><span class="line">        <span class="built_in">dfs2</span>(son[u],tp);<span class="comment">//优先遍历重儿子，为什么之后再说</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;g[u].<span class="built_in">size</span>();i++)&#123;</span><br><span class="line">            <span class="type">int</span> v=g[u][i];</span><br><span class="line">            <span class="keyword">if</span>(son[u]!=v&amp;&amp;f[u]!=v)&#123;<span class="comment">//遍历轻儿子</span></span><br><span class="line">                <span class="built_in">dfs2</span>(v,v);<span class="comment">//轻儿子一定是一条重链的链顶</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><br><strong>如果优先遍历重儿子，那么重链的$dfn$一定是连续的。</strong>例如：</p>
<p><img src="https://cdn.luogu.com.cn/upload/image_hosting/3w1daj6g.png" alt=""></p>
<p>因为重链的$dfn$是连续的，而每个点都属于一条重链，所以可以用线段树维护区间的方式维护点权，这样就不用暴力的一个个查，一个个改了。</p>
<p>一些常见的用法：<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">query</span>(<span class="number">1</span>,<span class="number">1</span>,n,dfn[top[u]],dfn[u])<span class="comment">//查询u到链顶的点权和</span></span><br><span class="line"><span class="built_in">modify</span>(<span class="number">1</span>,<span class="number">1</span>,n,dfn[top[u]],dfn[u],<span class="number">3</span>)<span class="comment">//把u到链顶的点权都加3</span></span><br></pre></td></tr></table></figure></p>
<p>具体到题目上，可以发现甚至连懒惰标记都不需要，没有区间修改的操作。</p>
<p>那么，怎么计算从一个点到另外一个点路径上的点权和？<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">query_ans</span><span class="params">(<span class="type">int</span> u,<span class="type">int</span> v)</span></span>&#123;</span><br><span class="line">    <span class="type">int</span> ret=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span>(top[u]!=top[v])&#123;</span><br><span class="line">        <span class="keyword">if</span>(deep[top[u]]&lt;deep[top[v]])&#123;<span class="comment">//注意，一定要比较链顶深度！坑了我好几次</span></span><br><span class="line">            <span class="built_in">swap</span>(u,v);</span><br><span class="line">        &#125;</span><br><span class="line">        ret^=<span class="built_in">query</span>(<span class="number">1</span>,<span class="number">1</span>,n,dfn[top[u]],dfn[u]);<span class="comment">//这道题要求异或</span></span><br><span class="line">        u=f[top[u]];</span><br><span class="line">    &#125;<span class="comment">//就是当uv不在同一条链上时，让链顶深度小的往上跳</span></span><br><span class="line">    <span class="keyword">if</span>(deep[u]&gt;deep[v])&#123;</span><br><span class="line">        <span class="built_in">swap</span>(u,v);</span><br><span class="line">    &#125;</span><br><span class="line">    ret^=<span class="built_in">query</span>(<span class="number">1</span>,<span class="number">1</span>,n,dfn[u],dfn[v]);<span class="comment">//当在同一条链上时，把它们之间的点加起来</span></span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>知道了这些操作，这题就非常好写了。就是直接把板子套上去嘛！</p>
<p>AC Code：<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAXN 200005</span></span><br><span class="line"><span class="type">int</span> n,q,e[MAXN];</span><br><span class="line">vector&lt;<span class="type">int</span>&gt; g[MAXN];</span><br><span class="line"><span class="type">int</span> dfn[MAXN],step,top[MAXN],size[MAXN],son[MAXN],f[MAXN],deep[MAXN];</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">dfs1</span><span class="params">(<span class="type">int</span> u,<span class="type">int</span> fa)</span></span>&#123;</span><br><span class="line">    size[u]=<span class="number">1</span>;</span><br><span class="line">    f[u]=fa;</span><br><span class="line">    deep[u]=deep[fa]+<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;g[u].<span class="built_in">size</span>();i++)&#123;</span><br><span class="line">        <span class="type">int</span> v=g[u][i];</span><br><span class="line">        <span class="keyword">if</span>(v!=fa)&#123;</span><br><span class="line">            <span class="built_in">dfs1</span>(v,u);</span><br><span class="line">            size[u]+=size[v];</span><br><span class="line">            <span class="keyword">if</span>(size[v]&gt;size[son[u]])&#123;</span><br><span class="line">                son[u]=v;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">dfs2</span><span class="params">(<span class="type">int</span> u,<span class="type">int</span> tp)</span></span>&#123;</span><br><span class="line">    top[u]=tp;</span><br><span class="line">    dfn[u]=++step;</span><br><span class="line">    <span class="keyword">if</span>(son[u])&#123;</span><br><span class="line">        <span class="built_in">dfs2</span>(son[u],tp);</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;g[u].<span class="built_in">size</span>();i++)&#123;</span><br><span class="line">            <span class="type">int</span> v=g[u][i];</span><br><span class="line">            <span class="keyword">if</span>(son[u]!=v&amp;&amp;f[u]!=v)&#123;</span><br><span class="line">                <span class="built_in">dfs2</span>(v,v);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> tree[MAXN*<span class="number">4</span>];</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">push_up</span><span class="params">(<span class="type">int</span> rt)</span></span>&#123;</span><br><span class="line">    tree[rt]=tree[rt*<span class="number">2</span>]^tree[rt*<span class="number">2</span>+<span class="number">1</span>];</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">modify</span><span class="params">(<span class="type">int</span> rt,<span class="type">int</span> l,<span class="type">int</span> r,<span class="type">int</span> x,<span class="type">int</span> k)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(l==r)&#123;</span><br><span class="line">        tree[rt]=k;</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="type">int</span> mid=(l+r)/<span class="number">2</span>;</span><br><span class="line">        <span class="keyword">if</span>(x&lt;=mid)&#123;</span><br><span class="line">            <span class="built_in">modify</span>(rt*<span class="number">2</span>,l,mid,x,k);</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="built_in">modify</span>(rt*<span class="number">2</span>+<span class="number">1</span>,mid+<span class="number">1</span>,r,x,k);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">push_up</span>(rt);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">query</span><span class="params">(<span class="type">int</span> rt,<span class="type">int</span> l,<span class="type">int</span> r,<span class="type">int</span> L,<span class="type">int</span> R)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(L&gt;R)&#123;<span class="keyword">return</span> <span class="number">0</span>;&#125;</span><br><span class="line">    <span class="keyword">if</span>(L&lt;=l&amp;&amp;R&gt;=r)&#123;</span><br><span class="line">        <span class="keyword">return</span> tree[rt];</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="type">int</span> mid=(l+r)/<span class="number">2</span>,ret=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">if</span>(L&lt;=mid)&#123;</span><br><span class="line">            ret^=<span class="built_in">query</span>(rt*<span class="number">2</span>,l,mid,L,R);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(R&gt;mid)&#123;</span><br><span class="line">            ret^=<span class="built_in">query</span>(rt*<span class="number">2</span>+<span class="number">1</span>,mid+<span class="number">1</span>,r,L,R);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ret;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">query_ans</span><span class="params">(<span class="type">int</span> u,<span class="type">int</span> v)</span></span>&#123;</span><br><span class="line">    <span class="type">int</span> ret=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span>(top[u]!=top[v])&#123;</span><br><span class="line">        <span class="keyword">if</span>(deep[top[u]]&lt;deep[top[v]])&#123;</span><br><span class="line">            <span class="built_in">swap</span>(u,v);</span><br><span class="line">        &#125;</span><br><span class="line">        ret^=<span class="built_in">query</span>(<span class="number">1</span>,<span class="number">1</span>,n,dfn[top[u]],dfn[u]);</span><br><span class="line">        u=f[top[u]];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(deep[u]&gt;deep[v])&#123;</span><br><span class="line">        <span class="built_in">swap</span>(u,v);</span><br><span class="line">    &#125;</span><br><span class="line">    ret^=<span class="built_in">query</span>(<span class="number">1</span>,<span class="number">1</span>,n,dfn[u],dfn[v]);</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>,&amp;n,&amp;q);</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,e+i);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n<span class="number">-1</span>;i++)&#123;</span><br><span class="line">        <span class="type">int</span> u,v,w;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>,&amp;u,&amp;v);</span><br><span class="line">        g[u].<span class="built_in">push_back</span>(v);</span><br><span class="line">        g[v].<span class="built_in">push_back</span>(u);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">dfs1</span>(<span class="number">1</span>,<span class="number">0</span>);</span><br><span class="line">    <span class="built_in">dfs2</span>(<span class="number">1</span>,<span class="number">1</span>);</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        <span class="built_in">modify</span>(<span class="number">1</span>,<span class="number">1</span>,n,dfn[i],e[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=q;i++)&#123;</span><br><span class="line">        <span class="type">int</span> op;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;op);</span><br><span class="line">        <span class="keyword">if</span>(op==<span class="number">1</span>)&#123;</span><br><span class="line">            <span class="type">int</span> x,k;</span><br><span class="line">            <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>,&amp;x,&amp;k);</span><br><span class="line">            <span class="built_in">modify</span>(<span class="number">1</span>,<span class="number">1</span>,n,dfn[x],k);</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="type">int</span> u,v;</span><br><span class="line">            <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>,&amp;u,&amp;v);</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,<span class="built_in">query_ans</span>(u,v));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>完结撒花~</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/08/28/%E6%A0%91%E9%93%BE%E5%89%96%E5%88%86%E8%AF%A6%E8%A7%A3%20AND%20%E9%A2%98%E8%A7%A3%20P6098%20%E3%80%90[USACO19FEB]Cow%20Land%20G%E3%80%91/" data-id="clk9o8ln7000e8wi5c8z21goh" data-title="树链剖分详解 AND 题解 P6098 【[USACO19FEB]Cow Land G】" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/08/28/%E4%B8%A7%E5%BF%83%E7%97%85%E7%8B%82%E7%9A%84%E5%8E%8B%E8%A1%8C/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          丧心病狂的压行
        
      </div>
    </a>
  
  
    <a href="/2020/08/26/%E6%A8%A1%E6%8B%9F%E9%80%80%E7%81%AB%E8%AF%A6%E8%A7%A3%20AND%E3%80%8CP1433%E3%80%8D%E9%A2%98%E8%A7%A3/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">模拟退火详解 AND P1433题解</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/10/">October 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/">决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解</a>
          </li>
        
          <li>
            <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/">CSP-S 2022 游记</a>
          </li>
        
          <li>
            <a href="/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">斜率优化动态规划 学习笔记</a>
          </li>
        
          <li>
            <a href="/2022/05/22/PKUSC%E4%B8%80%E5%8F%A5%E8%AF%9D%E6%B8%B8%E8%AE%B0/">PKUSC一句话游记</a>
          </li>
        
          <li>
            <a href="/2022/05/18/%E7%9C%81%E9%80%89%E6%B8%B8%E8%AE%B0%EF%BC%9F%E7%9C%81%E9%80%89%E6%B8%B8%E5%AF%84%EF%BC%81/">省选游记？省选游寄！</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>